<?php

namespace Frieza\Modules\Version\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Layout\Content;
use Frieza\Modules\Topic\Extensions\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class IosAppstoreVerController extends Controller
{

    private static $iosAppstoreVer = '/home/adm_rsync_dir/ios_appstore_ver.json';

    public function index(Request $request, Content $content){
        $data = self::getInfo();
        $title = 'Ios版本审核';
        $form = new Form($data);
        $form->text('ios_check_ver','ios_appstore_ver');
        $form->action('/admin/version/iosappstorever/index/save');
        return $content
            ->header($title)
            ->breadcrumb(
                [
                    'text' => $title,
                    'url' => '/admin/version/iosappstorever/index'
                ]
            )
            ->body($form);
    }

    public function save(Request $request){
        $request = $request->all();
        unset($request['_token']);
        $result = file_put_contents(self::$iosAppstoreVer,json_encode($request));
        if ($result) {
            $success = new MessageBag([
                'title' => '保存成功'
            ]);
            return back()->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '保存失败'
            ]);
            return back()->with(compact('error'));
        }
    }

    private static function getInfo(){
        if(file_exists(self::$iosAppstoreVer)){
            $conf = file_get_contents(self::$iosAppstoreVer);
            return json_decode($conf,true);
        }
        return [];
    }
    
}
